home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
cmln0386.arc
/
MATCH3.PRO
< prev
next >
Wrap
Text File
|
1986-03-03
|
1KB
|
49 lines
% Answer to the Prolog challenge from the AI Eye column in the March 1986
% issue of Computer Language. This version of Match implements pattern
% variables of the form [=,name], as in:
%
% Pattern Example Match?
%
% [a,[=,x],c] [a,b,c] Yes, x=b
% [[=,y],[=,y]] [a,a] Yes, x=a
% [[=,y],[=,y]] [a,b] No
%
% No, the code isn't commented. Can you figure out why it works and how
% it differs from the version of Match published in March? Answers (and
% comments!) in May.
append([],L,L).
append([X|L1],L2,[X|L3]) :- append(L1,L2,L3).
member(X,[X|_]).
member(X,[_|Y]) :- member(X,Y).
bind([N | V], B, B) :-
member([N | V], B).
bind([N | V], B, [[N | V] | B]) :-
not(member([N | X], B)).
match(P,E) :- match(P,E,[],_).
match([],[],B,B).
match([EH | PT], [EH | ET], B1, B) :-
match(PT,ET,B1,B).
match([+ | PT], E, B1, B) :-
append([EH | _],ET,E),
match(PT,ET,B1,B).
match([? | PT], [EH | ET], B1, B) :-
match(PT,ET,B1,B).
match([[=, N] | PT], [EH | ET], B1, B) :-
bind([N,EH], B1, B2),
match(PT,ET,B2,B).
match([PH | PT], [EH | ET], B1, B) :-
match(PH,EH,B1,B2),
match(PT,ET,B2,B).